package weaver.interfaces.autolink.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.autolink.utils.AutoLinkUtil;
import weaver.interfaces.autolink.utils.MailUtils;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 定薪流程提交后自动发送offer至候选人邮箱
 * @ModifyLog:
 **/
public class WeaDxlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaDxlcAction>>>>>requestid=" + requestid);
        try {
            String sql = "select id,yx,offersc,yjnr,yjbt from " + tablename + " where requestid=?";
            rs.executeQuery(sql, requestid);
            rs.next();
            String email = Util.null2String(rs.getString("yx"));
            String offersc = Util.null2String(rs.getString("offersc"));
            String context = Util.null2String(rs.getString("yjnr"));
            String title = Util.null2String(rs.getString("yjbt"));

            Map<String, String> filesMap = AutoLinkUtil.getFileInfo(offersc);
            String imagefileid = Util.null2String(filesMap.get("imagefileid"));
            String imagefilename = Util.null2String(filesMap.get("imagefilename"));
            writeLog(">>>>>WeaDxlcAction>>>>>imagefileid=" + imagefileid + ",imagefilename=" + imagefilename);
            String filetype = imagefilename.substring(imagefilename.lastIndexOf("."));
            writeLog(">>>>>WeaDxlcAction>>>>>filetype=" + filetype);
            String distFilePath = "E:\\WEAVER\\ecology\\offerfile\\" + imagefileid + filetype;
            writeLog(">>>>>WeaDxlcAction>>>>>distFilePath=" + distFilePath);
            boolean fileStat = AutoLinkUtil.createFile(Util.getIntValue(imagefileid), distFilePath);
            if (fileStat) {
                boolean status = MailUtils.sendMail(email, title, context, distFilePath, imagefilename);
                if (status) {
                    return Action.SUCCESS;
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("Action接口异常:邮件发送失败");
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("Action接口异常:获取附件失败");
                return Action.FAILURE_AND_CONTINUE;
            }

        } catch (Exception e) {
            e.printStackTrace();
            writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}

